//
//  SISSPassengerCooperationDetailViewController.m
//  SISSAPP
//
//  Created by 优房网 on 2017/4/24.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSPassengerCooperationDetailViewController.h"
#import "SISSMorePopupView.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "SISSChatViewController.h"
#import "SISSNetworkingManager+Home.h"
#import "SISSNetworkingManager+Mine.h"
#import "SISSRequestCooperationView.h"
#import "SISSInfomationViewController.h"
#import "SISSPassengerResourceCooperationViewController.h"

@interface SISSPassengerCooperationDetailViewController ()<SISSMorePopupViewDelegate,SISSRequestCooperationViewDelegate>

@property (weak, nonatomic) IBOutlet UILabel *nameAndPurposeLabel;
@property (weak, nonatomic) IBOutlet UILabel *regionAndCommunityLabel;
@property (weak, nonatomic) IBOutlet UITextField *budgetTextField;
@property (weak, nonatomic) IBOutlet UITextField *houseTypeTextField;
@property (weak, nonatomic) IBOutlet UITextField *areaTextField;
@property (weak, nonatomic) IBOutlet UITextField *intentionFloorTextField;
@property (weak, nonatomic) IBOutlet UITextField *decorationTextField;
@property (weak, nonatomic) IBOutlet UITextField *directionTextField;
@property (weak, nonatomic) IBOutlet UITextField *purchasePurposeTextField;
@property (weak, nonatomic) IBOutlet UILabel *otherDescriptionLabel;
@property (weak, nonatomic) IBOutlet UILabel *releaseTimeLabel;

@property (weak, nonatomic) IBOutlet UILabel *ownerPercentLabel;
@property (weak, nonatomic) IBOutlet UILabel *partnerPercentLabel;
@property (weak, nonatomic) IBOutlet UILabel *cooperationDescriptionLabel;

// 发布方信息
@property (weak, nonatomic) IBOutlet UIView *publisherView;
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *companyNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;
@property (weak, nonatomic) IBOutlet UIImageView *starLevelImageView;
@property (weak, nonatomic) IBOutlet UILabel *comment_countLabel;
@property (weak, nonatomic) IBOutlet UILabel *house_countLabel;
@property (weak, nonatomic) IBOutlet UILabel *passenger_countLabel;


@property (weak, nonatomic) IBOutlet UIView *consultAndRequestView;
@property (weak, nonatomic) IBOutlet UIView *onlineConsultButton;
@property (weak, nonatomic) IBOutlet UIView *requestCooperationButton;

@end

@implementation SISSPassengerCooperationDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"客源合作详情";
    
    // 判断是否显示发布方信息和咨询按钮
    self.publisherView.hidden = !self.isFindCooperation;
    self.consultAndRequestView.hidden = !self.isFindCooperation;
    self.consultAndRequestView.hidden = self.isHideConsultAndRequestView ?self.isHideConsultAndRequestView : !self.isFindCooperation;
    // 添加右侧分享删除按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"more..."] style:UIBarButtonItemStylePlain target:self action:@selector(shareAndComplainClick)];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    // 添加左侧的返回按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(backActon)];
}

- (void)backActon {
    
    if (self.isFindCooperation) {
        [self.navigationController popViewControllerAnimated:YES];
        return;
    }
    
    for (UIViewController *controller in self.navigationController.viewControllers) {
        
        if ([controller isKindOfClass:[SISSPassengerResourceCooperationViewController class]]) {
            [self.navigationController popToViewController:controller animated:YES];
        }
    }
}



- (void)setPassengerCooperationModel:(SISSPassengerCooperationDetailModel *)passengerCooperationModel {
    [self.view layoutIfNeeded];
    _passengerCooperationModel = passengerCooperationModel;
    
    self.releaseTimeLabel.text = [passengerCooperationModel.cooperation_insert_time substringToIndex:10];
    self.ownerPercentLabel.text = [NSString stringWithFormat:@"%zd%@",passengerCooperationModel.commission_rate,@"%"];
    self.partnerPercentLabel.text = [NSString stringWithFormat:@"%zd%@",100 - passengerCooperationModel.commission_rate,@"%"];
    self.cooperationDescriptionLabel.text = passengerCooperationModel.cooperation_desc;
    
    if (!self.isFindCooperation) return;
    // 根据身份证判断显示 先生、女士
    NSString *fisrtName = [passengerCooperationModel.buyer_name substringToIndex:1];
    NSInteger idNum = [[passengerCooperationModel.idNumber substringWithRange:NSMakeRange(passengerCooperationModel.idNumber.length - 2, 1)] integerValue];
    
    NSString *genderStr = (idNum % 2) == 0 ? @"女士" : @"先生";
    
    self.nameAndPurposeLabel.text = [NSString stringWithFormat:@"%@%@  %@",fisrtName,genderStr,@"求购"];
    self.regionAndCommunityLabel.text = [NSString stringWithFormat:@"%@-%@ %@",passengerCooperationModel.region,passengerCooperationModel.regionDetail,passengerCooperationModel.neighbourhood];
    self.budgetTextField.text = [NSString stringWithFormat:@"%.0f-%.0f万",passengerCooperationModel.minPrice,passengerCooperationModel.maxPrice];
    self.houseTypeTextField.text = passengerCooperationModel.houseType;
    self.areaTextField.text = [NSString stringWithFormat:@"%.0f-%.0f㎡",passengerCooperationModel.minArea,passengerCooperationModel.maxArea];
    self.intentionFloorTextField.text = (passengerCooperationModel.minFloor != 0 && passengerCooperationModel.maxFloor != 0) ? [NSString stringWithFormat:@"%zd-%zd层",passengerCooperationModel.minFloor,passengerCooperationModel.maxFloor] : @"暂无数据";;
    self.decorationTextField.text = passengerCooperationModel.decoration.length ? passengerCooperationModel.decoration : @"暂无数据";
    self.directionTextField.text = passengerCooperationModel.direction.length ? passengerCooperationModel.direction : @"暂无数据";
    self.purchasePurposeTextField.text = passengerCooperationModel.purpose.length ? passengerCooperationModel.purpose : @"暂无数据";
    self.otherDescriptionLabel.text = passengerCooperationModel.otherdesc.length ? passengerCooperationModel.otherdesc : @"暂无数据";
    
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:passengerCooperationModel.portrait] placeholderImage:[UIImage imageNamed:@"头像"]];
    self.nameLabel.text = passengerCooperationModel.name;
    self.companyNameLabel.text = passengerCooperationModel.shortName.length ? passengerCooperationModel.shortName : passengerCooperationModel.organName;
    self.scoreLabel.text = [NSString stringWithFormat:@"%zd",passengerCooperationModel.score];
    self.starLevelImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"star_%zd",passengerCooperationModel.star_level]];
    self.comment_countLabel.text = [NSString stringWithFormat:@"共%zd人评价",passengerCooperationModel.comment_count];
    self.house_countLabel.text = [NSString stringWithFormat:@"%zd",passengerCooperationModel.house_count];
    self.passenger_countLabel.text = [NSString stringWithFormat:@"%zd",passengerCooperationModel.demand_count];
}

- (void)setPassengerModel:(SISSPassengerResourceModel *)passengerModel {
    [self.view layoutIfNeeded];
    _passengerModel = passengerModel;
    
    if (self.isFindCooperation) return;
    
    self.nameAndPurposeLabel.text = [NSString stringWithFormat:@"%@求购",passengerModel.name];
    self.regionAndCommunityLabel.text = [NSString stringWithFormat:@"%@-%@ %@",passengerModel.region,passengerModel.regionDetail,passengerModel.neighbourhood];
    self.budgetTextField.text = [NSString stringWithFormat:@"%.0f-%.0f万",passengerModel.minPrice,passengerModel.maxPrice];
    self.houseTypeTextField.text = passengerModel.houseType;
    self.areaTextField.text = [NSString stringWithFormat:@"%.0f-%.0f㎡",passengerModel.minArea,passengerModel.maxArea];
    self.intentionFloorTextField.text = (passengerModel.minFloor != 0 && passengerModel.maxFloor != 0) ? [NSString stringWithFormat:@"%zd-%zd层",passengerModel.minFloor,passengerModel.maxFloor] : @"暂无数据";;
    self.decorationTextField.text = passengerModel.decoration.length ? passengerModel.decoration : @"暂无数据";
    self.directionTextField.text = passengerModel.direction.length ? passengerModel.direction : @"暂无数据";
    self.purchasePurposeTextField.text = passengerModel.purpose.length ? passengerModel.purpose : @"暂无数据";
    self.otherDescriptionLabel.text = passengerModel.otherdesc.length ? passengerModel.otherdesc : @"暂无数据";

}

/**
 分享投诉按钮事件
 */
- (void)shareAndComplainClick {
    
    NSArray *titles = self.isFindCooperation ? @[@"分享", @"举报"] : @[@"分享"];
    SISSMorePopupView *moreView = [[SISSMorePopupView alloc] initWithTitles:titles];
    moreView.delegate = self;
    [moreView show];
}

#pragma mark - SISSMorePopupViewDelegate
- (void)morePopupViewSelectedWithIndex:(NSInteger)index {
    
    if (index == 0) {
        // 根据身份证判断显示 先生、女士
        NSString *fisrtName = [self.passengerCooperationModel.name substringToIndex:1];
        NSInteger idNum = [[self.passengerCooperationModel.idNumber substringWithRange:NSMakeRange(self.passengerCooperationModel.idNumber.length - 2, 1)] integerValue];
        
        NSString *genderStr = (idNum % 2) == 0 ? @"女士" : @"先生";
        NSString *title = [NSString stringWithFormat:@"%@%@ 求购",fisrtName,genderStr];
        NSString *descr = [NSString stringWithFormat:@"%@-%@ | %@-%@ | %@",@(self.passengerCooperationModel.minPrice),@(self.passengerCooperationModel.maxPrice),self.passengerCooperationModel.region,self.passengerCooperationModel.regionDetail,self.passengerCooperationModel.houseType];
        NSString *webpageUrl = @"";
        if (self.passengerCooperationModel.demandType == 1) { // 线上
            webpageUrl = [NSString stringWithFormat:@"%@/?#!/buyerDetail/%@/",WEB_BASE_URL,self.passengerCooperationModel.abdId];
        } else { // 线下
            webpageUrl = [NSString stringWithFormat:@"%@/?#!/offlineDemandDetail/%@",WEB_BASE_URL,self.passengerCooperationModel.abdId];
        }
        
        [[SISSPublicSingleton shared] shareWithTitle:title descr:descr webpageUrl:webpageUrl currentViewController:self thumImage:nil];
    } else {
        
        SISSReportViewController *vc = [[SISSReportViewController alloc] init];
        vc.isReportPassenger = YES;
        SISSPassengerResourceModel *passengerModel = [[SISSPassengerResourceModel alloc] init];
        passengerModel.demandType = self.passengerCooperationModel.demandType;
        passengerModel.ID = self.passengerCooperationModel.abdId;
        vc.passengerModel = passengerModel;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}



#pragma mark - 中介信息查询点击按钮
- (IBAction)checkInformationTapAction:(id)sender {
    
    [kNetworkManager requestPersonInfoWithSpId:self.passengerCooperationModel.spId success:^(SISSPersonInfoModel *personInfo) {
        
        SISSInfomationViewController *vc = [[UIStoryboard storyboardWithName:@"Mine" bundle:nil] instantiateViewControllerWithIdentifier:@"SISSInfomationViewController"];
        vc.personInfo = personInfo;
        
        vc.spId = self.passengerCooperationModel.spId;
        [self.navigationController pushViewController:vc animated:YES];
        
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];

    
}


/**
 在线咨询按钮点击事件
 */
- (IBAction)onlineConsultButtonClickAction:(id)sender {
    
    NSLog(@"在线咨询按钮点击事件");
    
    SISSChatViewController *chatVC = [[SISSChatViewController alloc] initWithConversationChatter:self.passengerCooperationModel.imAccount conversationType:EMConversationTypeChat];
    SISSContactModel *model = [[SISSContactModel alloc] init];
    model.name = self.passengerCooperationModel.name;
    model.portrait = self.passengerCooperationModel.portrait;
    model.spId = self.passengerCooperationModel.spId;
    model.accountType = 1;
    chatVC.contactModel = model;
    chatVC.title =self.passengerCooperationModel.name;
    [self.navigationController pushViewController:chatVC animated:YES];
}


/**
 请求合作按钮点击事件
 */
- (IBAction)requestCooperationButtonCooperationClickAction:(id)sender {
    
    SISSRequestCooperationView *requestView = [[SISSRequestCooperationView alloc] init];
    requestView.delegate = self;
     requestView.cooperationTextStr = @"我对您发布的客源合作很感兴趣想和您进一步沟通，请求同意合作。";
    NSLog(@"点击请求合作按钮");
}

#pragma mark - SISSRequestCooperationViewDelegate
- (void)requestCooperationViewClickConfirmButtonWithCooperationReason:(NSString *)cooperationReason {
    
    
    
    NSDictionary *parameters = @{
                                 @"spId":[SISSUserAccount shared].spId,
                                 @"adcrId":self.passengerCooperationModel.ID,
                                 @"cooperation_desc":cooperationReason
                                 };
    
    [kNetworkManager requestPassengerCooperationRequestWithparameters:parameters success:^(id responseObject) {
        
        if ([responseObject[@"code"] integerValue] == 200) { // 发布成功
            [SISSTools showSVSuccessTitle:@"请求合作成功" andTime:2 andColor:YES];
            EMTextMessageBody *body = [[EMTextMessageBody alloc] initWithText:cooperationReason];
            
            EMMessage *message = [[EMMessage alloc] initWithConversationID:self.passengerCooperationModel.imAccount from:[SISSUserAccount shared].imAccount to:self.passengerCooperationModel.imAccount body:body ext:nil];
            [[EMClient sharedClient].chatManager sendMessage:message progress:nil completion:^(EMMessage *message, EMError *error) {
                
                
            }];
        }
        
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}


@end
